home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / pgp23src.zip / PGPPAGER.C < prev    next >
C/C++ Source or Header  |  1993-03-07  |  5KB  |  205 lines

  1.  
  2. /* pgppager, designed to be possibly integrated with elm mail reader
  3.  * 
  4.  * by Alessandro Bottonelli (c)1993, A.Bottonelli@it12.bull.it
  5.  * version 1.0 - 29/Jan/93
  6.  *
  7.  * This program is put in the public domain and permission is granted
  8.  * to use it for any purpose, provided this copyright notice is retained.
  9.  * NO WARRANTIES expressed or implied are given. USE THIS PROGRAM AT
  10.  * YOUR OWN RISK!
  11.  *
  12.  * This programs reads from a specified file or from stdin if no file is
  13.  * specified and creates three temporary files (header, encrypted, and 
  14.  * trailer) as needed, in order to store the header portion in cleartext,
  15.  * the encrypted portion still in cypher text, and the trailer portion of
  16.  * the clear text. Then, if applicable, the cleartext header is outputted,
  17.  * the encrypted portion is piped through pgp as needed, then the trailer
  18.  * (if any) is outputted. THIS PROCESS IS TRANSPARENT TO NON PGP ENCRYPTED
  19.  * TEXTS
  20.  *
  21.  * For use with ELM, it is necessary to specify the display to be:
  22.  *
  23.  *    <whatever_path>/pgppager | pg
  24.  *
  25.  * in the elm option menu.
  26.  * 
  27.  */
  28.  
  29. #include <stdio.h>
  30.  
  31. /*
  32.  * you may need to customize the next DEFINEs
  33.  */
  34.  
  35. #define PGP "/usr/local/bin/pgp -f"
  36. #define BEGIN "-----BEGIN PGP MESSAGE---"
  37. #define END "-----END PGP MESSAGE---"
  38.  
  39.  
  40. #define TEMP "/usr/tmp"        /* Temporary dir of your choice       */
  41. #define CAT "cat -s"        /* -s option to avoid cat complaining */
  42.                 /* about missing files                */
  43.  
  44. /*
  45.  * program should be invoked as "pgppager [file] | pg"
  46.  * actually no check is made to verify that the program
  47.  * is piped through "pg" or "more", just in case someone
  48.  * wants to redirect it to a file or a printer.
  49.  * BEWARE: redirecting it to a file or printer may be
  50.  * not good for your security if the file or printer level
  51.  * of security is unknown or poor ...
  52.  */
  53.  
  54. /*
  55.  * This three variables global to be visible to the Exit() routine
  56.  */
  57.  
  58. char fh[128];    /* Header temporary file        */
  59. char fe[128];    /* Encrypted temporary file     */
  60. char fr[128];    /* Trailer temporary file       */
  61.  
  62. main(ac,av)
  63. int ac;
  64. char **av;
  65. {
  66.     FILE *fp;    /* File pointer to read file(s) */
  67.     FILE *ft;    /* File pointer to temp file(s) */
  68.     char line[256]; /* Read/Write buffer            */
  69.     char cmd[128];    /* command string               */
  70.  
  71.  
  72.  
  73. /* 
  74.  * Must be invoked as "pgppager [file_name]"
  75.  */
  76.  
  77.     if ( ac > 2 )
  78.     {
  79.         fprintf(stderr,"Usage: %s [file_name]\n", av[0]);
  80.         Exit(1);
  81.     }
  82.  
  83. /*
  84.  * File ( if specified ) must exist and be readable
  85.  */
  86.  
  87.     if ( ac == 2 )
  88.     {
  89.         if ( ( fp = fopen( av[1], "r") ) == NULL )
  90.         {
  91.             fprintf(stderr,"%s: cannot read %s\n", av[0], av[1]);
  92.             Exit(2);
  93.         }
  94.     }
  95.     else
  96.         fp=stdin;    /* if no file specified assume standard input */
  97.  
  98. /*
  99.  * Build temporary file names
  100.  *
  101.  * please note how, as files are created for writing, they are
  102.  * also chmod(ed) to 0600. I expect most PGP users to have their
  103.  * umask set to 077, but just to be on the safe side ... :-)
  104.  */
  105.  
  106.     sprintf ( fh, "%s/pgpm.head.%d", TEMP, (int )getpid() );
  107.     sprintf ( fe, "%s/pgpm.encr.%d", TEMP, (int )getpid() );
  108.     sprintf ( fr, "%s/pgpm.trai.%d", TEMP, (int )getpid() );
  109.  
  110.  
  111. /* 
  112.  * READ file until Eof, redirect to "fe"  only those portions of the
  113.  * file that are between the BEGIN PGP and END PGP MESSAGE lines.
  114.  */
  115.     if ( ( ft = fopen ( fh, "w") ) == NULL )
  116.     {
  117.         fprintf(stderr,"%s: error opening temp file\n", av[0]);
  118.         Exit(4);
  119.     }
  120.     
  121.     if ( (int )chmod( fh, 0600) != 0 )
  122.     {
  123.         fprintf(stderr,"%s: cannot chmod temp file\n", av[0]);
  124.         Exit(5);
  125.     }
  126.  
  127.     fprintf( ft, "\n");    /* Just to start with a blank line */
  128.  
  129.     while ( fgets( line, 128, fp) != NULL )
  130.     {
  131.         if ( !strncmp( BEGIN, line, strlen( BEGIN) ))    
  132.         {
  133.             fprintf(ft, "\n===pgp encrypted message begins===\n");
  134.             fclose(ft);
  135.  
  136.             if ( ( ft = fopen ( fe, "w") ) == NULL )
  137.             {
  138.                 fprintf(stderr,"%s: error opening temp file\n", av[0]);
  139.                 Exit(4);
  140.             }
  141.  
  142.             if ( (int )chmod( fe, 0600) != 0 )
  143.             {
  144.                 fprintf(stderr,"%s: cannot chmod temp file\n", av[0]);
  145.                 Exit(5);
  146.             }
  147.         }
  148.  
  149.         fprintf( ft, "%s", line);
  150.  
  151.         if ( !strncmp( END, line, strlen( END) ))
  152.         {
  153.             fclose(ft);
  154.  
  155.             if ( ( ft = fopen ( fr, "w") ) == NULL )
  156.             {
  157.                 fprintf(stderr,"%s: error opening temp file\n", av[0]);
  158.                 Exit(4);
  159.             }
  160.  
  161.             if ( (int )chmod( fr, 0600) != 0 )
  162.             {
  163.                 fprintf(stderr,"%s: cannot chmod temp file\n", av[0]);
  164.                 Exit(5);
  165.             }
  166.  
  167.             fprintf(ft, "\n===pgp encrypted message ends===\n");
  168.         }
  169.     }
  170.     
  171.     fclose(fp);
  172.     fclose(ft);    
  173.  
  174.     sprintf( cmd, "%s %s", CAT, fh);
  175.     system(cmd);
  176.     sprintf( cmd, "%s %s | %s", CAT, fe, PGP);
  177.     system(cmd);
  178.     sprintf( cmd, "%s %s", CAT, fr);
  179.     system(cmd);
  180.  
  181.     Exit(0);    /* That's all folks ... */
  182. }
  183.  
  184. /*
  185.  * Exit(s) are centralized in this routine to make sure we wipe out
  186.  * temporary files in any case ...
  187.  */
  188.  
  189. Exit(n)
  190. int n;        /* value to be returned to the shell (0 = OK) */
  191. {
  192. /*
  193.  * Remove temporary files (if any)
  194.  * and sync() just for the sake of it
  195.  */
  196.  
  197.     unlink(fh);
  198.     unlink(fe);
  199.     unlink(fr);
  200.     sync();
  201.  
  202.     exit(n);
  203. }
  204.  
  205.